<template>
    <div class="classification">
        <el-form :inline="true">
            <el-form-item>
                <el-button @click="$router.push({ name: 'dishes' })">返回</el-button>
                <el-button type="primary" @click="addOrUpdateHandle()">新增</el-button>
            </el-form-item>
        </el-form>
        <el-table :data="dataList" border>
            <el-table-column prop="classifyId" header-align="center" align="center" label="分类ID">
            </el-table-column>
            <el-table-column prop="classifyName" header-align="center" align="center" label="分类名">
            </el-table-column>
            <el-table-column fixed="right" header-align="center" align="center" width="230" label="操作">
                <template slot-scope="scope">
                    <el-button type="text" size="small" @click="addOrUpdateHandle(scope.row.classifyId)">修改</el-button>
                    <el-button type="text" size="small" @click="deleteHandle(scope.row.classifyId)">删除</el-button>
                </template>
            </el-table-column>
        </el-table>
        <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getDataList"></add-or-update>
    </div>
</template>

<script>
import AddOrUpdate from './class-add-or-update'
export default {
    data() {
        return {
            dataList: [],
            addOrUpdateVisible: false
        }
    },
    activated() {
        this.getDataList()
    },
    components: {
        AddOrUpdate
    },
    methods: {
        getDataList() {
            this.$http({
                url: this.$http.adornUrl(`/web/classfication/list`),
                method: 'get',
            }).then(({ data }) => {
                console.log(data)
                this.dataList = data.list
            })
        },
        addOrUpdateHandle(id) {
            this.addOrUpdateVisible = true
            this.$nextTick(() => {
                this.$refs.addOrUpdate.init(id)
            })
        },
        deleteHandle(id) {
            this.$confirm(`确定对[id=${id}]进行[${id ? '删除' : '批量删除'}]操作?`, '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then((data) => {
                this.$http({
                    url: this.$http.adornUrl(`/web/classfication/delete/${id}`),
                    method: 'Delete',
                }).then(({ data }) => {
                    if (data && data.code === 0) {
                        this.$message({
                            message: '操作成功',
                            type: 'success',
                            onClose: () => {
                                this.getDataList()
                            }
                        })
                    } else {
                        this.$message.error(data.msg)
                    }
                }).catch(() => { })
            }
            )

        }
    }
}

</script>

<style></style>